import {Vec3 } from 'cc';
import { nUtils } from './nUtils';
export class Channel {
  
    constructor(public portals = [],public path= new Array){

    }

    push(p1:Vec3, p2:Vec3 = null) {
        if (p2 == null) p2 = p1;
        this.portals.push({
            left: p1,
            right: p2
        });
    };

  
    stringPull() {
        const portals = this.portals;
        const pts = [];
        // Init scan state
        let portalApex, portalLeft, portalRight;
        let apexIndex = 0,
            leftIndex = 0,
            rightIndex = 0;
    
        portalApex = portals[0].left;
        portalLeft = portals[0].left;
        portalRight = portals[0].right;
    
        // Add start point.
        pts.push(portalApex);
    
        for (let i = 1; i < portals.length; i++) {
          const left = portals[i].left;
          const right = portals[i].right;
    
          // Update right vertex.
          if (nUtils.triarea2(portalApex, portalRight, right) <= 0.0) {
            if (nUtils.vequal(portalApex, portalRight) || nUtils.triarea2(portalApex, portalLeft, right) > 0.0) {
              // Tighten the funnel.
              portalRight = right;
              rightIndex = i;
            } else {
              // Right over left, insert left to path and restart scan from portal left point.
              pts.push(portalLeft);
              // Make current left the new apex.
              portalApex = portalLeft;
              apexIndex = leftIndex;
              // Reset portal
              portalLeft = portalApex;
              portalRight = portalApex;
              leftIndex = apexIndex;
              rightIndex = apexIndex;
              // Restart scan
              i = apexIndex;
              continue;
            }
          }
    
          // Update left vertex.
          if (nUtils.triarea2(portalApex, portalLeft, left) >= 0.0) {
            if (nUtils.vequal(portalApex, portalLeft) || nUtils.triarea2(portalApex, portalRight, left) < 0.0) {
              // Tighten the funnel.
              portalLeft = left;
              leftIndex = i;
            } else {
              // Left over right, insert right to path and restart scan from portal right point.
              pts.push(portalRight);
              // Make current right the new apex.
              portalApex = portalRight;
              apexIndex = rightIndex;
              // Reset portal
              portalLeft = portalApex;
              portalRight = portalApex;
              leftIndex = apexIndex;
              rightIndex = apexIndex;
              // Restart scan
              i = apexIndex;
              continue;
            }
          }
        }
    
        if ((pts.length === 0) || (!nUtils.vequal(pts[pts.length - 1], portals[portals.length - 1].left))) {
          // Append last point to path.
          pts.push(portals[portals.length - 1].left);
        }
    
        this.path = pts;

        // 去除起始位置
        this.path.shift();
  
    }
};